#ifndef __DBMSGPROCDISPATCH_H__
#define __DBMSGPROCDISPATCH_H__

#include <iostream>
#include "helper/msgprocdispatcher.hpp"
#include "IMessageOperator.h"

/**
*@file       dbmsgprocdispatch.h
*@brief      database application
*
*@author     graywind
*@version    1.0.0.0
*@data       2011/12/27
*/

///@brief  listen event dispatch
class ListenHandler
{
public:
        static void OnDispatch(IMessageOperator* p, int session_id, void* pMsgBuffer,
                               unsigned long msgID, unsigned long msg_bytes, int error_code)
        {
                if(error_code == 0)
                {
                        std::cout<<"A New Session Connected!"<<std::endl;
                        p->SendNotify(session_id);
                }
                else
                {
					std::cout<<"Can Not Accept A New Session! Error Code:"<<error_code<<std::endl;
                }
        }
};
RegisterMsgProcDispatcher(IMessageOperator, Listen, ListenHandler);

/***************************************************************************************************************************/
///@brief    connect event  dispatch
class ConnectHandler
{
public:
        static void OnDispatch(IMessageOperator* p, int session_id, void* pMsgBuffer,
                               unsigned long msgID, unsigned long msg_bytes, int error_code)
        {
                if(error_code == 0)
                {
                        std::cout<<"Connect To Server Success!"<<std::endl;
                }
                else
                {
                        std::cout<<"Can Not Connect To Server! Error Code:"<<error_code<<std::endl;
                }
        }
};
RegisterMsgProcDispatcher(IMessageOperator, Connect, ConnectHandler);

/***************************************************************************************************************************/
///@brief    kick event  dispatch
class KickHandler
{
public:
        static void OnDispatch(IMessageOperator* p, int session_id, void* pMsgBuffer,
                               unsigned long msgID, unsigned long msg_bytes, int error_code)
        {
			    std::cout<<"Kick Session:"<<session_id<<std::endl;
        }
};
RegisterMsgProcDispatcher(IMessageOperator, Kick, KickHandler);

/***************************************************************************************************************************/
///@brief    disconnect event  dispatch
class DisconnectHandler
{
public:
        static void OnDispatch(IMessageOperator* p, int session_id, void* pMsgBuffer,
                               unsigned long msgID, unsigned long msg_bytes, int error_code)
        {
			    std::cout<<"Session:"<<session_id<<" Disconnect!"<<std::endl;
        }
};
RegisterMsgProcDispatcher(IMessageOperator, Disconnect, DisconnectHandler);

/***************************************************************************************************************************/
///@brief   send event dispatch
class SendHandler
{
public:
        static void OnDispatch(IMessageOperator* p, int session_id, void* pMsgBuffer,
                               unsigned long msgID, unsigned long msg_bytes, int error_code)
        {
                if(error_code != 0)
                {
					std::cout<<"Send Message Error! Error Code:"<<error_code<<std::endl;
                }
        }
};
RegisterMsgProcDispatcher(IMessageOperator, Send, SendHandler);

/***************************************************************************************************************************/
///@brief    read event dispatch
class ReadHandler
{
public:
        static void OnDispatch(IMessageOperator* p, int session_id, void* pMsgBuffer,
                               unsigned long msgID, unsigned long msg_bytes, int error_code)
        {
                if(error_code == 0)
                {
					p->StoreMessage(session_id, pMsgBuffer, msgID, msg_bytes);
                }
                else
                {
					std::cout<<"Read Message Error! Error Code:"<<error_code<<std::endl;
                }
        }
};
RegisterMsgProcDispatcher(IMessageOperator, Read, ReadHandler);

#endif
